<script setup>
import { delCateApi, getCateListApi } from '@/api'
import { ref } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
import ChannelEdit from './components/ChannelEdit.vue'
import { ElMessage } from 'element-plus'

const channelList = ref([])
const load = ref(false)
const dialogEdit = ref()

const getCateList = async () => {
  load.value = true
  const res = await getCateListApi()
  channelList.value = res.data.data
  load.value = false
}
getCateList()

const EditChannel = (row) => {
  dialogEdit.value.openEdit(row)
}
const addChannel = () => {
  // console.log(dialogEdit.value)
  dialogEdit.value.openEdit({})
}
const getSuccess = () => {
  getCateList()
}
const deleteChannel = async (row) => {
  await ElMessageBox.confirm('确认要删除吗？', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await delCateApi(row.id)
  ElMessage.success('删除成功')
  getCateList()
}
</script>
<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="addChannel">添加分类</el-button>
    </template>
    <el-table :data="channelList" style="width: 100%" v-loading="load">
      <el-table-column label="序号" width="180" type="index" />
      <el-table-column prop="cate_name" label="分类名称" width="180" />
      <el-table-column prop="cate_alias" label="分类别名" />
      <el-table-column label="操作">
        <template #default="{ row, $index }">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            plain
            @click="EditChannel(row, $index)"
          />
          <el-button
            type="danger"
            :icon="Delete"
            circle
            plain
            @click="deleteChannel(row)"
          />
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <channel-edit ref="dialogEdit" @success="getSuccess"></channel-edit>
  </page-container>
</template>

<style lang="scss" scoped></style>
